package cn.b.sky.flowable.service.impl;

import cn.b.sky.flowable.service.CustomGroupAndRoleService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.user.dao.OrganizationDao;
import cn.b.sky.user.dao.RoleDao;
import cn.b.sky.user.dao.UserDao;
import cn.b.sky.user.service.model.Organization;
import cn.b.sky.user.service.model.Role;
import cn.b.sky.user.service.model.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("customGroupAndRoleService")
public class CustomGroupAndRoleServiceImpl implements CustomGroupAndRoleService {
  private static final Logger log= LoggerFactory.getLogger(CustomGroupAndRoleServiceImpl.class);

  @Autowired
  private UserDao userDao;

  @Autowired
  private OrganizationDao orgDao;

  @Autowired
  private RoleDao roleDao;

  @Override
  public List<String> findGroupsAndRolesStrByUser(String userCode, String siteCode) {
    log.debug("findGroupsByUser : {}", userCode + siteCode);

    List<String> groups = new ArrayList<String>();
    // 用户组织
    List<Organization> orgs = userDao.selectOrganizationByUser(siteCode, userCode);
    for (Organization org : orgs) {
      groups.add(SystemConstant.USER_ORG + org.getOrganizationCode());
    }
    List<Role> roles = userDao.selectRoleByUser(siteCode, userCode);
    for (Role role : roles) {
      groups.add(SystemConstant.USER_ROLE + role.getRoleCode());
    }
    return groups;
  }

  @Override
  public String findGroupsTextByCode(String code, String siteCode) {
    if (!StringUtils.isBlank(code)) {
      StringBuilder sb = new StringBuilder();
      for (String org : code.split(",")) {
        if (org.indexOf(SystemConstant.USER_ORG) != -1) {
          org = org.replaceAll(SystemConstant.USER_ORG, "");
          Organization organization = orgDao.selectOrganization(siteCode, org);
          if (organization != null) {
            sb.append(organization.getOrganizationText()).append(",");
          }
        }
      }
      String orgs = sb.toString();
      orgs = orgs.endsWith(",") ? orgs.substring(0, orgs.length() - 1) : orgs;
      return orgs;
    }
    return null;
  }

  @Override
  public String findRolesTextByCode(String code, String siteCode) {
    if (!StringUtils.isBlank(code)) {
      StringBuilder sb = new StringBuilder();
      for (String roleCode : code.split(",")) {
        if (roleCode.indexOf(SystemConstant.USER_ROLE) != -1) {
          roleCode = roleCode.replaceAll(SystemConstant.USER_ROLE, "");
          Role role = roleDao.selectRole(siteCode, roleCode);
          if (role != null) {
            sb.append(role.getRoleText()).append(",");
          }
        }
      }
      String roles = sb.toString();
      roles = roles.endsWith(",") ? roles.substring(0, roles.length() - 1) : roles;
      return roles;
    }
    return null;
  }

  @Override
  public String findUsersTextByCode(String code, String siteCode) {
    if (!StringUtils.isBlank(code)) {
      StringBuilder sb = new StringBuilder();
      for (String userCode : code.split(",")) {
        UserInfo user = userDao.selectUser(siteCode, userCode);
        if (user != null) {
          sb.append(user.getUserText()).append(",");
        }
      }
      String userTexts = sb.toString();
      userTexts = userTexts.endsWith(",") ? userTexts.substring(0, userTexts.length() - 1) : userTexts;
      return userTexts;
    }
    return null;
  }
}
